﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TableOCR
{
    public partial class FormSnapShot : Form
    {
        public FormSnapShot()
        {
            InitializeComponent();
        }

        private int intStartX = 0;
        private int intStartY = 0;
        private bool isMouseDraw = false;

        private void pictureBoxSrc_MouseDown(object sender, MouseEventArgs e)
        {
            isMouseDraw = true;

            intStartX = e.X;
            intStartY = e.Y;
        }

        private void pictureBoxSrc_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDraw)
            {
                try
                {

                    Graphics g = this.pictureBoxSrc.CreateGraphics();
                    //清空上次画下的痕迹
                    g.Clear(this.pictureBoxSrc.BackColor);
                    int StartX = intStartX > e.X ? e.X : intStartX;
                    int StartY = intStartY > e.Y ? e.Y : intStartY;
                    int ScreenWidth = Math.Abs(e.X - intStartX);
                    int ScreenHeight = Math.Abs(e.Y - intStartY);

                    //去除选框外闪烁
                    SolidBrush soldgray = new SolidBrush(Color.Gray);
                    g.FillRectangle(soldgray, 0, 0, pictureBoxSrc.Width, StartY);//清除上
                    g.FillRectangle(soldgray, 0, StartY + ScreenHeight, pictureBoxSrc.Width, pictureBoxSrc.Height - (StartY + ScreenHeight));//清除下
                    g.FillRectangle(soldgray, 0, StartY, StartX, ScreenHeight + 1);//清除左
                    g.FillRectangle(soldgray, StartX + ScreenWidth, StartY, pictureBoxSrc.Width - (StartX + ScreenWidth), ScreenHeight + 1);//清除右

                    //选框里
                    SolidBrush soldwhite = new SolidBrush(Color.White);
                    Rectangle rec = new Rectangle(StartX, StartY, ScreenWidth, ScreenHeight);
                    g.FillRectangle(soldwhite, rec);

                    //选框
                    Brush brush = new SolidBrush(Color.Red);
                    Pen pen = new Pen(brush, 1);
                    pen.DashStyle = DashStyle.Solid;
                    g.DrawRectangle(pen, new Rectangle(StartX, StartY, ScreenWidth, ScreenHeight));

                    g.Dispose();
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }
        }

        private void pictureBoxSrc_MouseUp(object sender, MouseEventArgs e)
        {

            int StartX = intStartX > e.X ? e.X : intStartX;
            int StartY = intStartY > e.Y ? e.Y : intStartY;
            int ScreenWidth = Math.Abs(e.X - intStartX);
            int ScreenHeight = Math.Abs(e.Y - intStartY);

            Bitmap myImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics g = Graphics.FromImage(myImage);
            g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height));
            g.Dispose();

            this.Snapshot = myImage.Clone(new Rectangle(StartX + 1, StartY + 1, ScreenWidth - 1, ScreenHeight - 1), System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            isMouseDraw = false;
            intStartX = 0;
            intStartY = 0;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        public Bitmap Snapshot { get; set; }

    }
}
